package org.wanghong.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.wanghong.basic.service.impl.BaseServiceImpl;
import org.wanghong.basic.util.PageList;
import org.wanghong.org.domain.Department;
import org.wanghong.org.mapper.DepartmentMapper;
import org.wanghong.org.query.DepartmentQuery;
import org.wanghong.org.service.IDepartmentService;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    /*@Autowired
    private DepartmentMapper mapper;

    @Transactional
    @Override
    public void add(Department department) {
        mapper.save(department);
    }
    @Transactional
    @Override
    public void del(Serializable id) {
        mapper.remove(id);
      *//*  int i=1/0;*//*

    }

    @Transactional
    @Override
    public void update(Department department) {
        mapper.update(department);
    }

    @Override
    public Department getById(Serializable id) {
        return mapper.loadById(id);
    }

    @Override
    public List<Department> getAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList<Department> queryPage(DepartmentQuery query) {
        long total = mapper.loadCount(query);//总条数
        if(total<1)
            return new PageList<>();

        List<Department> rows = mapper.loadData(query);

        return new PageList<>(total,rows);

    }*/

    @Override
    public void update(Department department) {
        Long deptId = department.getId();
        //生成path
        String path = "";
        if (department.getParent()==null){
            //1 判断是否是顶级部门，如果是，自己生成  /id
            path = "/"+deptId;
        }else{
            //2 否则parentPath+/id
            Long pid = department.getParent().getId();
            String pPath = super.getById(pid).getPath();
            path = pPath+"/"+deptId;
        }
        department.setPath(path);
        department.setUpdate_time(new Date());

        super.update(department);
    }

    @Override
    public void add(Department department) {
        department.setCreate_time(new Date()); //只是设置值
        super.add(department); //在xml中配置了保存完就返回id
        Long deptId = department.getId();
        //生成path
        String path = "";
        if (department.getParent()==null || department.getParent().getId()==null){
            //1 判断是否是顶级部门，如果是，自己生成  /id
            path = "/"+deptId;
        }else{
            //2 否则parentPath+/id
            Long pid = department.getParent().getId();
            String pPath = super.getById(pid).getPath();
            System.out.println(pPath);
            path = pPath+"/"+deptId;
            System.out.println(path);
        }
        department.setPath(path);
        System.out.println(department.getPath());
        department.setUpdate_time(new Date());
        super.update(department);
    }

    @Override
    public List<Department> getTree() {
        return departmentMapper.loadTree();
    }


}
